package cn.plugins.generator.task

import cn.plugins.generator.utils.GenerateCodeUtil
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

/**
 * 创建于 2020-01-08 16:37
 *
 * @author jiangyun
 * @类说明 任务类
 */
open class GenerateCodeTask : DefaultTask() {

    lateinit var dbType: String                 // 数据库类型
    lateinit var url: String                    // 数据库连接地址
    lateinit var port: String                   // 端口
    lateinit var database: String               // 数据库名称
    lateinit var userName: String               // 用户名
    lateinit var password: String               // 密码
    lateinit var frameworkType: String          // 框架类型(mybatis或mybatisplus)
    lateinit var tablePrefix: String            // 表前缀
    lateinit var tableName: String              // 表名称，多个使用英文逗号隔开
    lateinit var projectPath: String            // 项目绝对路径
    lateinit var packageName: String            // 包名称
    lateinit var controller: String             // 控制器文件夹名称
    lateinit var service: String                // 业务层文件夹名称
    lateinit var mapper: String                 // dao层文件夹名称
    lateinit var entity: String                 // 实体文件夹名称
    lateinit var xml: String                    // dao层对应xml文件夹名称
    lateinit var languageType: String           // 语言类型，目前只支持java和kotlin
    lateinit var author: String                 // 作者
    lateinit var company: String                // 所属公司
    lateinit var apiPrefix: String              // 接口前缀
    var isRestfulStyle: Boolean = true          // 是否生成restful接口
    var enableGenerateDoc: Boolean = true       // 是否允许生成doc
    var enableGenVuePage: Boolean = false       // 是否生成vue页面
    //lateinit var isServerH2: Int              // 是否为独立服务h2数据库

    @TaskAction
    fun generateAction() {
        if (dbType.isBlank()) {
            println("database type can't be null or empty")
            return
        }
        /*if (dbType != "mysql" || dbType != "postgresql") {
            printlnln("database type only surport mysql or postgresql")
            return
        }*/
        if (url.isBlank()) {
            println("url can't be null or empty")
            return
        }
        if (port.isBlank()) {
            println("port can't be null or empty")
            return
        }
        if (database.isBlank()) {
            println("databaseName can't be null or empty")
            return
        }
        if (userName.isBlank()) {
            println("userName can't be null or empty")
            return
        }
        if (password.isBlank()) {
            println("password can't be null or empty")
            return
        }
        if (frameworkType.isBlank()) {
            println("password can't be null or empty")
            return
        }
        if (tablePrefix.isBlank()) {
            println("frameworkType can't be null or empty")
            return
        }
        if (tableName.isBlank()) {
            println("tableName can't be null or empty")
            return
        }
        if (projectPath.isBlank()) {
            println("projectPath can't be null or empty")
            return
        }
        if (packageName.isBlank()) {
            println("packageName can't be null or empty")
            return
        }
        if (controller.isBlank()) {
            println("controller can't be null or empty")
            return
        }
        if (service.isBlank()) {
            println("service can't be null or empty")
            return
        }
        if (mapper.isBlank()) {
            println("mapper can't be null or empty")
            return
        }
        if (entity.isBlank()) {
            println("entity can't be null or empty")
            return
        }
        if (xml.isBlank()) {
            println("xml can't be null or empty")
            return
        }
        if (languageType.isBlank()) {
            println("languageType can't be null or empty")
            return
        }
        if (author.isBlank()) {
            println("author can't be null or empty")
            return
        }
        if (company.isBlank()) {
            println("company can't be null or empty")
            return
        }
        if (languageType != "java" && languageType != "kotlin") {
            println("only surpport generate java or kotlin code")
            return
        }
        if (apiPrefix.isBlank()) {
            apiPrefix = "api"
            println("default api prefix is:/api")
        }

        url = when(dbType) {
            "mysql" -> "jdbc:mysql://${url}:${port}/${database}?useUnicode=true&serverTimezone=Asia/Shanghai&useSSL=false&characterEncoding=UTF-8&allowPublicKeyRetrieval=true&autoReconnect=true&failOverReadOnly=false&tinyInt1isBit=false"
            "postgresql" -> "jdbc:postgresql://${url}:${port}/${database}"
            // "h2" -> "jdbc:h2:"
            else -> ""
        }

        try {
            println("========================== start generate code ==========================")
            println("========================== author: iwiteks-jy  ==========================")
            println("========================== version: 1.0.4      ==========================")
            GenerateCodeUtil.generateCode(
                dbType,
                url,
                database,
                userName,
                password,
                frameworkType,
                tablePrefix,
                tableName,
                projectPath,
                packageName,
                controller,
                service,
                mapper,
                entity,
                xml,
                languageType,
                author,
                company,
                apiPrefix,
                isRestfulStyle,
                enableGenerateDoc,
                enableGenVuePage
            )
        } catch (ex: Exception) {
            println(ex)
        } finally {
            println("========================== end generate code ==========================")
        }
    }
}